package com.hellobike.haxing.modules.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.product.entity.ProductTag;
import com.hellobike.haxing.modules.product.mapper.ProductTagMapper;
import com.hellobike.haxing.modules.product.service.IProductTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 商品标签
 * @Author: haxing-boot
 * @Date:   2020-02-12
 * @Version: V1.0
 */
@Service
public class ProductTagServiceImpl extends ServiceImpl<ProductTagMapper, ProductTag> implements IProductTagService {

    @Autowired
    private ProductTagMapper productTagMapper;

    @Override
    public Result addTagProducts(Long tagId, List<Long> productIdList) {
        for (Long productId : productIdList) {
            QueryWrapper<ProductTag> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(ProductTag::getTagId, tagId).eq(ProductTag::getProductId, productId);
            ProductTag exsitProductTag = productTagMapper.selectOne(queryWrapper);
            if (exsitProductTag == null) {
                ProductTag productTag = new ProductTag();
                productTag.setTagId(tagId);
                productTag.setProductId(productId);
                productTagMapper.insert(productTag);
            }
        }
        return Result.ok();
    }

}
